﻿using UnityEngine;
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;


public static class Debugger
{
    private static TextWriter textWriter = null;
    private static bool isStart = false;

    static Debugger()
    {
        Start();
    }

    static void Start()
    {
        string path = Application.persistentDataPath + "/Debugger.log";
        bool flag = false;

        if (File.Exists(path))
        {
            FileInfo fi = new FileInfo(path);
            float len = fi.Length;
            len /= (1024 * 1024);
            flag = len > 4f ? false : true;
        }

        textWriter = new StreamWriter(path, flag, Encoding.UTF8);
        Log("Debugger start");
    }

    public static void ShutDown()
    {
        isStart = false;
        Application.RegisterLogCallback(null);    

        textWriter.Flush();
        textWriter.Close();
        textWriter = null;
    }

    public static void Write(string str)
    {
        textWriter.WriteLine(str);
        textWriter.Flush();
    }

    public static void Log(string str, params object[] param)
    {
        string s = string.Format(str, param);
        Debug.Log(string.Format("[{0}]: {1}", DateTime.Now.ToString("hh:mm:ss.fff"), s));
    }


    public static void LogWarning(string str, params object[] param)
    {
        string s = string.Format(str, param);
        Debug.LogWarning(string.Format("[{0}]: {1}", DateTime.Now.ToString("hh:mm:ss.fff"), s));
    }

    public static void LogError(string str, params object[] param)
    {
        string s = string.Format(str, param);
        Debug.LogError(string.Format("[{0}]: {1}", DateTime.Now.ToString("hh:mm:ss.fff"), s));
    }
}
